package xhhx.manager;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class DataManager {
    private static final Configuration configuration = new Configuration().configure();
    private static final SessionFactory sessionFactory = configuration.buildSessionFactory();
    private static final Session session = sessionFactory.openSession();

    public static void Init() {
    }

    public static void Close() {
        session.close();
        sessionFactory.close();
    }

    public static <T> T Get(int id, Class<T> tClass) {
        T data = session.get(tClass, id);
        return data;
    }

    public static <T> void Insert(T data) {
        session.beginTransaction();
        session.save(data);
        session.getTransaction().commit();
    }

    public synchronized static <T> void Update(T data) {
        session.beginTransaction();
        session.update(data);
        session.getTransaction().commit();
    }

    public static <T> void Delete(T data) {
        session.beginTransaction();
        session.delete(data);
        session.getTransaction().commit();
    }
}
